package cn.tqfeiyang.smartadmin.system.controller;

import cn.tqfeiyang.smartadmin.commons.pagination.QueryParams;
import cn.tqfeiyang.smartadmin.system.response.PermissionQueryVO;
import cn.tqfeiyang.smartadmin.system.service.MenuService;
import cn.tqfeiyang.smartadmin.system.request.MenuCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.MenuUpdateDTO;
import cn.tqfeiyang.smartadmin.system.request.MenuQueryDTO;
import cn.tqfeiyang.smartadmin.system.response.MenuQueryVO;
import cn.tqfeiyang.smartadmin.system.response.MenuDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.Set;

/**
 * 菜单控制器
 *
 * @author tqfeiyang
 * @since 2024-08-23
 */
@RestController
@Validated
@RequestMapping("/api/system/menu")
@Tag(name = "系统/菜单")
public class MenuController {

    /**
     * 菜单服务
     */
    @Resource
    private MenuService menuService;

    /**
     * 查询菜单
     *
     * @param dto 请求数据
     * @return 菜单列表
     */
    @OperationLog
    @GetMapping("/query")
    @Operation(summary = "查询菜单")
    @SaCheckPermission("system:menu:query")
    public ApiResult<PageData<MenuQueryVO>> queryMenus(@Validated MenuQueryDTO dto) {
        return ApiResult.success(menuService.queryMenus(dto));
    }

    /**
     * 获取菜单
     *
     * @param menuId 菜单id
     * @return 菜单信息
     */
    @OperationLog
    @GetMapping("/{menuId}/read")
    @Operation(summary = "获取菜单")
    @SaCheckPermission("system:menu:read")
    public ApiResult<MenuDetailVO> readMenu(@PathVariable @EntityId Long menuId) {
        return ApiResult.success(menuService.readMenu(menuId));
    }

    /**
     * 添加菜单
     *
     * @param dto 请求数据
     * @return 菜单信息
     */
    @OperationLog
    @PostMapping("/create")
    @Operation(summary = "添加菜单")
    @SaCheckPermission("system:menu:create")
    public ApiResult<MenuDetailVO> createMenu(@RequestBody @Validated MenuCreateDTO dto) {
        Long menuId = menuService.createMenu(dto);
        return ApiResult.success(menuService.readMenu(menuId));
    }

    /**
     * 修改菜单
     *
     * @param menuId 菜单id
     * @param dto 请求数据
     * @return 菜单信息
     */
    @OperationLog
    @PostMapping("/{menuId}/update")
    @Operation(summary = "修改菜单")
    @SaCheckPermission("system:menu:update")
    public ApiResult<MenuDetailVO> updateMenu(@PathVariable @EntityId Long menuId, @RequestBody @Validated MenuUpdateDTO dto) {
        menuService.updateMenu(menuId, dto);
        return ApiResult.success(menuService.readMenu(menuId));
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单id
     */
    @OperationLog
    @PostMapping("/{menuId}/delete")
    @Operation(summary = "删除菜单")
    @SaCheckPermission("system:menu:delete")
    public ApiResult<String> deleteMenu(@PathVariable @EntityId Long menuId) {
        menuService.deleteMenu(menuId);
        return ApiResult.success();
    }

    /**
     * 查询角色
     *
     * @param menuId 菜单id
     */
    @OperationLog
    @GetMapping("/{menuId}/roles/query")
    @Operation(summary = "查询角色")
    @SaCheckPermission("system:menu:roles:query")
    public ApiResult<PageData<PermissionQueryVO>> queryRoles(@PathVariable @EntityId Long menuId, @Validated QueryParams dto) {
        return ApiResult.success(menuService.queryRoles(menuId, dto));
    }

    /**
     * 修改角色
     *
     * @param menuId 菜单id
     * @param roleIds 角色id集合
     */
    @OperationLog
    @PostMapping("/{menuId}/roles/update")
    @Operation(summary = "修改角色")
    @SaCheckPermission("system:menu:roles:update")
    public ApiResult<String> updateRoles(@PathVariable @EntityId Long menuId, @RequestBody Set<Long> roleIds) {
        menuService.updateRoles(menuId, roleIds);
        return ApiResult.success();
    }
}
